package com.qixin.util.md5;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author liaojialin
 * @version 1.0.0
 * @ClassName Md5Utils
 * @Description
 * @Date 2017年5月26日 下午2:49:00
 */
public class Md5Util {
    public static final String MD5 = "MD5";
    public static final int WCHAT = 1;
    public static final int WEB = 2;


    public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        //确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
        return newstr;
    }

    /**
     * 得到一个加密后的byte数组
     * @param str
     * @param algorithm
     * @return
     */
    public static byte[] EncryptionStr(String str, String algorithm) {
        // 加密之后所得字节数组
        byte[] bytes = null;
        try {
            // 获取MD5算法实例 得到一个md5的消息摘要
            MessageDigest md = MessageDigest.getInstance(algorithm);
            //添加要进行计算摘要的信息
            md.update(str.getBytes());
            //得到该摘要
            bytes = md.digest();
        } catch (NoSuchAlgorithmException e) {
            System.out.println("加密算法: " + algorithm + " 不存在: ");
        }
        return null == bytes ? null : bytes;
    }

    /**
     *
     * @param b
     * @return 处理MD5加密后的字符串，返回16位数字
     */
    public static String byteArrayToNumString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToSingleNum(b[i]));
        }
        return resultSb.toString();
    }

    /**
     * 处理得到的byte数组,使数组中的数字变成一位的
     * @param b
     * @return
     */
    private static String byteToSingleNum(byte b) {
        int _b = b;
        if (_b < 0) {
            _b = (256 + _b) % 10;
        } else {
            _b = _b % 10;
        }
        return String.valueOf(_b);
    }

    /**
     * @param bytes
     * @return 处理MD5加密后的字符串，32位byte长度
     */
    public static String BytesConvertToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (byte aByte : bytes) {
            String s = Integer.toHexString(0xff & aByte);
            if (s.length() == 1) {
                sb.append("0" + s);
            } else {
                sb.append(s);
            }
        }
        return sb.toString();
    }

    public static String parseCurrentTimeToMD5(String currentTime){
        System.out.println(currentTime);
        byte[] bytes = EncryptionStr(currentTime,MD5);
        String resultStr = byteArrayToNumString(bytes);
        return addInfoToMD5(resultStr,currentTime);
    }

    /**
     * 将原本的数据加入到加密后的数据中
     * @param originalStr
     * @param info
     * @return
     */
    public static String addInfoToMD5(String originalStr, String info){
        char[] originalChar = originalStr.toCharArray();
        StringBuffer resuleSb = new StringBuffer();
        for (int i = 0; i < originalChar.length; i++) {
            if (i == 3) {
                resuleSb.append(info);
            }
            resuleSb.append(originalChar[i]);
        }
        return resuleSb.toString();
    }

    
    /*public static String addInfoToMD5(String originalStr, String info){
        char[] originalChar = originalStr.toCharArray();
        StringBuffer resuleSb = new StringBuffer();
        for (int i = 0; i < originalChar.length; i++) {
            if (i == 3) {
                resuleSb.append(info);
            }
            resuleSb.append(originalChar[i]);
        }
        return resuleSb.toString();
    }*/
    
    
    public static String parseUserInfoToMD5(String userInfo) {
        byte[] bytes = EncryptionStr(userInfo, MD5);
        return addInfoToMD5(byteArrayToNumString(bytes),userInfo);
    }

    
 /*   public static String parseOpenIdToMD5(String openId) {
        // 加密之后所得字节数组
        byte[] resultByte = EncryptionStr(openId,MD5);
        String resultStr = BytesConvertToHexString(resultByte);

        return addInfoToMD5(resultStr,openId);
    }*/
    
    
    public static String parsePhoneNumToMD5(String phoneNum) {
           // 加密之后所得字节数组
            byte[] resultByte = EncryptionStr(phoneNum,MD5);
            String resultStr = BytesConvertToHexString(resultByte);

            return addInfoToMD5(resultStr,phoneNum);
    }
       

    public static String geturnToken(String info,String currentTime,int source){
    	//WCHAT = 1
        if (source == WCHAT){
//            return parseOpenIdToMD5(info)+'.'+parseCurrentTimeToMD5(currentTime);
              return parsePhoneNumToMD5(info)+'.'+parseCurrentTimeToMD5(currentTime);
        //WEB = 2    
        }else if (source == WEB){
            return parseUserInfoToMD5(info)+'.'+parseCurrentTimeToMD5(currentTime);
        }
        return null;
    }

    public static void main(String... args){
        // System.out.println(geturnToken("oz9D9w-gcGoZTueokQlA0VCSzqIA"));
       //System.out.println( getRandomPwd());;
    }
}
